package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.HumanResourcesBI;
import com.cleaningcloud.cld.domain.MaterialsBI;
import com.cleaningcloud.cld.domain.vo.CldBiDataVo;
import com.cleaningcloud.cld.mapper.MaterialsBIMapper;
import com.cleaningcloud.cld.service.IMaterialsBIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @className 资产物资BI业务实现类
 * @description //TODO
 * @author Shaw
 * @date: 2025-03-06 16:33
 * @param: //TODO
 */
@Service
public class MaterialsBIServiceImpl implements IMaterialsBIService {

    //系统报表映射
    @Autowired
    private MaterialsBIMapper  materialsBIMapper;

    /**
     * @Author Shaw
     * @Description 库存统计表格
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getStockStatisticsTable() {
        return materialsBIMapper.getStockStatisticsTable();
    }

    /**
     * @Author Shaw
     * @Description 物资类别库存数量占比图
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getCommodityTypeRate(){
        return materialsBIMapper.getCommodityTypeRate();
    }

    /**
     * @Author Shaw
     * @Description 按当月查询物资资产成本(日走势图)
     * @date: 2025-09-09 9:45
     * @param: //TODO
     */
    @Override
    public CldBiDataVo getMaterialsInfoCostDayImg(){
        CldBiDataVo cbv = new CldBiDataVo();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        BigDecimal[] dz = new BigDecimal[days];
        BigDecimal[] zj = new BigDecimal[days];
        BigDecimal[] wx = new BigDecimal[days];
        BigDecimal[] pv = new BigDecimal[days];
        BigDecimal[] rv = new BigDecimal[days];
        MaterialsBI bi = new MaterialsBI();
        bi.setDateYear(year);
        bi.setDateMonth(month);
        for (int i=1 ; i<=days ; i++) {
            bi.setDateDay(i);
            MaterialsBI obj = materialsBIMapper.getMaterialsInfoCostDayImg(bi);

            dz[i-1] = obj.getDz();
            zj[i-1] = obj.getZj();
            wx[i-1] = obj.getWx();
            pv[i-1] = obj.getPv();
            rv[i-1] = obj.getRv();
        }
        Object[] dataVal = new Object[5];
        dataVal[0] =  dz;
        dataVal[1] =  wx;
        dataVal[2] =  zj;
        dataVal[3] =  pv;
        dataVal[4] =  rv;
        cbv.setDataVal(dataVal);
        return cbv;
    }

    /**
     * @Author Shaw
     * @Description 按当月查询物资资产成本(月累计表格)
     * @date: 2025-09-09 9:45
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getMaterialsInfoCostTable(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        MaterialsBI materialsBI = new MaterialsBI();
        String dateVal = "";
        if(month < 10){
            dateVal = year + "0" + month;
        }else{
            dateVal = year + "" + month;
        }
        materialsBI.setDateVal(dateVal);
        return materialsBIMapper.getMaterialsInfoCostTable(materialsBI);
    }

    /**
     * @Author Shaw
     * @Description 采购订单商品类型库存数量占比
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getProcureCommodityTypeRate(){
        return materialsBIMapper.getProcureCommodityTypeRate();
    }

    /**
     * @Author Shaw
     * @Description 采购订单不同状态订单数表格
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getProcureNumberInfoTable(){
        return materialsBIMapper.getProcureNumberInfoTable();
    }

    /**
     * @Author Shaw
     * @Description 物资品类消耗占比
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getMaterialsConsumeRate(){
        return materialsBIMapper.getMaterialsConsumeRate();
    }

    /**
     * @Author Shaw
     * @Description 物资品类消耗数量表格
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getMaterialsConsumeTable(){
        return materialsBIMapper.getMaterialsConsumeTable();
    }

    /**
     * @Author Shaw
     * @Description 项目业态物资调拨单数量占比
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getMaterialsAllocateRate(){
        return materialsBIMapper.getMaterialsAllocateRate();
    }

    /**
     * @Author Shaw
     * @Description 配送订单不同状态订单数表格
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getMaterialsAllocateNumberInfoTable(){
        return materialsBIMapper.getMaterialsAllocateNumberInfoTable();
    }

    /**
     * @Author Shaw
     * @Description 三级库商品分类库存数量占比
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getLevelStockCommodityTypeRate(Long wareType){
        return materialsBIMapper.getLevelStockCommodityTypeRate(wareType);
    }

    /**
     * @Author Shaw
     * @Description 根据类型ID获取三级库存数量/金额占比
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getLevelCommodityTypeRateByCommodityTypeId(MaterialsBI materialsBI){
         return materialsBIMapper.getLevelCommodityTypeRateByCommodityTypeId(materialsBI);
    }

    /**
     * @Author Shaw
     * @Description 根据类型ID获取三级库存数量/金额TOP10
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getLevelCommodityTypeTableByCommodityTypeId(MaterialsBI materialsBI){
        return materialsBIMapper.getLevelCommodityTypeTableByCommodityTypeId(materialsBI);
    }

    /**
     * @Author Shaw
     * @Description 资产维修金额占比
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI>  getAssetsRepairRate(){
        return materialsBIMapper.getAssetsRepairRate();
    }

    /**
     * @Author Shaw
     * @Description 根据项目/区域查询资产状态信息表格
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getProjectAssetsInfoTable(Long paramType){
        List<MaterialsBI> list = materialsBIMapper.getProjectAssetsInfoTable(paramType);
        for (MaterialsBI materialsBI : list) {
            materialsBI.setWorthMoney(materialsBI.getOriginalMoney().subtract(materialsBI.getDepreciationMoney()));
            materialsBI.setRepairRate(materialsBI.getRepairMoney().divide(materialsBI.getSumRepairMoney(),2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)));
        }
        return list;
    }

    /**
     * @Author Shaw
     * @Description 根据资产查询资产状态信息列表
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getAssetsInfoList(){
        List<MaterialsBI> list = materialsBIMapper.getAssetsInfoList();
        for (MaterialsBI materialsBI : list) {
            materialsBI.setWorthMoney(materialsBI.getOriginalMoney().subtract(materialsBI.getDepreciationMoney()));
        }
        return list;
    }

    /**
     * @Author Shaw
     * @Description 按项目查询物资品类消耗数量图
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public CldBiDataVo getProjectRegionMaterialsTypeConsumeImg(){
        CldBiDataVo cbv = new CldBiDataVo();
        List<MaterialsBI> list = materialsBIMapper.getProjectRegionMaterialsTypeConsumeImg();
        String[] str = new String[list.size()];
        Integer[] qjgj = new Integer[list.size()];
        Integer[] qjyj = new Integer[list.size()];
        Integer[] ljcz = new Integer[list.size()];
        Integer[] czsn = new Integer[list.size()];
        Integer[] jxpj = new Integer[list.size()];
        Integer[] keyp = new Integer[list.size()];
        Integer[] lbyp = new Integer[list.size()];
        Integer[] qt = new Integer[list.size()];
        int i = 0;
        for (MaterialsBI materialsBI : list) {
            str[i] = materialsBI.getProjectRegion();
            qjgj[i] = materialsBI.getQjgjNumber();
            qjyj[i] = materialsBI.getQjyjNumber();
            ljcz[i] = materialsBI.getLjczNumber();
            czsn[i] = materialsBI.getCzsnNumber();
            jxpj[i] = materialsBI.getJxpjNumber();
            keyp[i] = materialsBI.getKeypNumber();
            lbyp[i] = materialsBI.getLbypNumber();
            qt[i] = materialsBI.getQtNumber();
            i++;
        }
        Object[] dataVal = new Object[9];
        dataVal[0] = str;
        dataVal[1] = qjgj;
        dataVal[2] = qjyj;
        dataVal[3] = ljcz;
        dataVal[4] = czsn;
        dataVal[5] = jxpj;
        dataVal[6] = keyp;
        dataVal[7] = lbyp;
        dataVal[8] = qt;
        cbv.setDataVal(dataVal);
        return cbv;
    }

    /**
     * @Author Shaw
     * @Description 按项目区域汇总物资品类消耗占比
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getMaterialsTypeConsumeRateByProjectRegion(Long commodityTypeId){
        return materialsBIMapper.getMaterialsTypeConsumeRateByProjectRegion(commodityTypeId);
    }

    /**
     * @Author Shaw
     * @Description 按物资消耗数量TOP10
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getMaterialsConsumeTopTable(Long commodityTypeId){
        return materialsBIMapper.getMaterialsConsumeTopTable(commodityTypeId);
    }

    /**
     * @Author Shaw
     * @Description 按项目查询物资品类消耗数量表格
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getProjectMaterialsTypeConsumeTable(){
        return  materialsBIMapper.getProjectMaterialsTypeConsumeTable();
    }

    /**
     * @Author Shaw
     * @Description 当月采购日度订单数量走势图
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public CldBiDataVo getProcureOrderByDayImg(){
        CldBiDataVo cbv = new CldBiDataVo();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        Integer[] sumNumber = new Integer[days];
        Integer[] successNumber = new Integer[days];
        Integer[] unSuccessNumber = new Integer[days];
        MaterialsBI bi = new MaterialsBI();
        for (int i=1 ; i<=days ; i++) {
            String dateVal = "";
            if(i < 10){
                if(month < 10){
                    dateVal = year + "0" + month + "0" + i;
                }else{
                    dateVal = year + "" + month + "0" + i;
                }
            }else{
                if(month < 10){
                    dateVal = year + "0" + month + "" + i;
                }else{
                    dateVal = year + "" + month + "" + i;
                }
            }
            bi.setDateVal(dateVal);
            MaterialsBI obj = materialsBIMapper.getProcureOrderByDayImg(bi);

            sumNumber[i-1] = obj.getSumNumber();
            successNumber[i-1] = obj.getSuccessNumber();
            unSuccessNumber[i-1] = obj.getUnSuccessNumber();
        }
        Object[] dataVal = new Object[3];
        dataVal[0] =  sumNumber;
        dataVal[1] =  successNumber;
        dataVal[2] =  unSuccessNumber;
        cbv.setDataVal(dataVal);
        return cbv;
    }

    /**
     * @Author Shaw
     * @Description 按供应商查询采购订单状态数量/金额表格
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getSupplierProjectOrderStatusInfoTable(){
        return materialsBIMapper.getSupplierProjectOrderStatusInfoTable();
    }

    /**
     * @Author Shaw
     * @Description 按物资品类区分采购数量占比
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getCommodityTypeProduceNumberRate(){
        return materialsBIMapper.getCommodityTypeProduceNumberRate();
    }

    /**
     * @Author Shaw
     * @Description 按物资品类区分采购金额占比
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getCommodityTypeProduceMoneyRate(){
        return materialsBIMapper.getCommodityTypeProduceMoneyRate();
    }

    /**
     * @Author Shaw
     * @Description 按物资品类查询采购订单状态数量/金额表格
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getSupplierCommodityTypeProjectOrderStatusInfoTable(){
        return materialsBIMapper.getSupplierCommodityTypeProjectOrderStatusInfoTable();
    };

    /**
     * @Author Shaw
     * @Description 配送订单项目区域调拨数量占比
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getProjectRegionAllocateRate(){
        return materialsBIMapper.getProjectRegionAllocateRate();
    }

    /**
     * @Author Shaw
     * @Description 当月采购日度订单数量走势图
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public CldBiDataVo getAllocateOrderByDayImg(){
        CldBiDataVo cbv = new CldBiDataVo();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        Integer[] sumNumber = new Integer[days];
        Integer[] successNumber = new Integer[days];
        Integer[] unSuccessNumber = new Integer[days];
        MaterialsBI bi = new MaterialsBI();
        for (int i=1 ; i<=days ; i++) {
            String dateVal = "";
            if(i < 10){
                if(month < 10){
                    dateVal = year + "0" + month + "0" + i;
                }else{
                    dateVal = year + "" + month + "0" + i;
                }
            }else{
                if(month < 10){
                    dateVal = year + "0" + month + "" + i;
                }else{
                    dateVal = year + "" + month + "" + i;
                }
            }
            bi.setDateVal(dateVal);
            MaterialsBI obj = materialsBIMapper.getAllocateOrderByDayImg(bi);

            sumNumber[i-1] = obj.getSumNumber();
            successNumber[i-1] = obj.getSuccessNumber();
            unSuccessNumber[i-1] = obj.getUnSuccessNumber();
        }
        Object[] dataVal = new Object[3];
        dataVal[0] =  sumNumber;
        dataVal[1] =  successNumber;
        dataVal[2] =  unSuccessNumber;
        cbv.setDataVal(dataVal);
        return cbv;
    }

    /**
     * @Author Shaw
     * @Description 按项目物资品类查询配送订单信息表格
     * @date: 2025-04-09 11:57
     * @param: //TODO
     */
    @Override
    public List<MaterialsBI> getCommodityTypeAllocateOrderStatusInfoTable(){
        List<MaterialsBI> list = materialsBIMapper.getProjectList();
        if(!list.isEmpty()){
            for(MaterialsBI mb : list){
                MaterialsBI obj = new MaterialsBI();
                obj.setProjectId(mb.getProjectId());

                //清洁工具
                obj.setCommodityTypeId(21L);
                Map<String, Object> map1 = materialsBIMapper.getAllocateListByCommodityType(obj);
                if(null != map1) {
                    mb.setQjgjNumber(map1.get("allocateNumber") == null ? 0 : Integer.parseInt(map1.get("allocateNumber").toString()));
                    mb.setQjgjPeakValue(map1.get("peakValue") == null ? 0 : Integer.parseInt(map1.get("peakValue").toString()));
                }else{
                    mb.setQjgjNumber(0);
                    mb.setQjgjPeakValue(0);
                }

                //清洁药剂
                obj.setCommodityTypeId(22L);
                Map<String, Object> map2 = materialsBIMapper.getAllocateListByCommodityType(obj);
                if(null != map2){
                    mb.setQjyjNumber(map2.get("allocateNumber") == null ? 0 :Integer.parseInt(map2.get("allocateNumber").toString()));
                    mb.setQjyjPeakValue(map2.get("peakValue") == null ? 0 : Integer.parseInt(map2.get("peakValue").toString()));
                }else{
                    mb.setQjyjNumber(0);
                    mb.setQjyjPeakValue(0);
                }

                //垃圾处置
                obj.setCommodityTypeId(23L);
                Map<String, Object> map3 = materialsBIMapper.getAllocateListByCommodityType(obj);
                if(null != map3) {
                    mb.setLjczNumber(map3.get("allocateNumber") == null ? 0 : Integer.parseInt(map3.get("allocateNumber").toString()));
                    mb.setLjczPeakValue(map3.get("peakValue") == null ? 0 : Integer.parseInt(map3.get("peakValue").toString()));
                }else{
                    mb.setLjczNumber(0);
                    mb.setLjczPeakValue(0);
                }

                //承载收纳
                obj.setCommodityTypeId(25L);
                Map<String, Object> map4 = materialsBIMapper.getAllocateListByCommodityType(obj);
                if(null != map4) {
                    mb.setCzsnNumber(map4.get("allocateNumber") == null ? 0 : Integer.parseInt(map4.get("allocateNumber").toString()));
                    mb.setCzsnPeakValue(map4.get("peakValue") == null ? 0 : Integer.parseInt(map4.get("peakValue").toString()));
                }else{
                    mb.setCzsnNumber(0);
                    mb.setCzsnPeakValue(0);
                }

                //机械配件
                obj.setCommodityTypeId(36L);
                Map<String, Object> map5 = materialsBIMapper.getAllocateListByCommodityType(obj);
                if(null != map5) {
                    mb.setJxpjNumber(map5.get("allocateNumber") == null ? 0 :Integer.parseInt(map5.get("allocateNumber").toString()));
                    mb.setJxpjPeakValue(map5.get("peakValue") == null ? 0 : Integer.parseInt(map5.get("peakValue").toString()));
                }else{
                    mb.setJxpjNumber(0);
                    mb.setJxpjPeakValue(0);
                }

                //客用品
                obj.setCommodityTypeId(28L);
                Map<String, Object> map6 = materialsBIMapper.getAllocateListByCommodityType(obj);
                if(null != map6) {
                    mb.setKeypNumber(map6.get("allocateNumber") == null ? 0 : Integer.parseInt(map6.get("allocateNumber").toString()));
                    mb.setKeypPeakValue(map6.get("peakValue") == null ? 0 : Integer.parseInt(map6.get("peakValue").toString()));
                }else{
                    mb.setKeypNumber(0);
                    mb.setKeypPeakValue(0);
                }

                //劳保用品
                obj.setCommodityTypeId(27L);
                Map<String, Object> map7 = materialsBIMapper.getAllocateListByCommodityType(obj);
                if(null != map7) {
                    mb.setLbypNumber(map7.get("allocateNumber") == null ? 0 :Integer.parseInt(map7.get("allocateNumber").toString()));
                    mb.setLbypPeakValue(map7.get("peakValue") == null ? 0 : Integer.parseInt(map7.get("peakValue").toString()));
                }else{
                    mb.setLbypNumber(0);
                    mb.setLbypPeakValue(0);
                }

                //其他
                obj.setCommodityTypeId(37L);
                Map<String, Object> map8 = materialsBIMapper.getAllocateListByCommodityType(obj);
                if(null != map8) {
                    mb.setQtNumber(map8.get("allocateNumber") == null ? 0 :Integer.parseInt(map8.get("allocateNumber").toString()));
                    mb.setQtPeakValue(map8.get("peakValue") == null ? 0 : Integer.parseInt(map8.get("peakValue").toString()));
                }else{
                    mb.setQtNumber(0);
                    mb.setQtPeakValue(0);
                }
            }
        }
        return list;
    };
}